using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class LeagueScroll : MonoBehaviour
{
	[SerializeField]
	private LeagueElement elementInScroll;

	private List<string> listNameLeague = new List<string>();

	private List<LeagueElement> listLeague = new List<LeagueElement>();

	[SerializeField]
	private UIGrid grid;

	[SerializeField]
	private UIScrollView scroll;

	private void Awake()
	{
		if (grid == null)
		{
			grid = base.transform.GetComponent<UIGrid>();
		}
		if (listLeague.Count == 0)
		{
			CoroutineRunner.Instance.StartCoroutine(LoadingAllElement());
		}
		if (CupsManager.CurrentLeagueIndex >= 8)
		{
			scroll.contentPivot = UIWidget.Pivot.Right;
		}
	}

	private void OnEnable()
	{
		if (listLeague.Count != 0)
		{
			RefreshLeague();
		}
	}

	private IEnumerator LoadingAllElement()
	{
		foreach (CupsInfo cupsInfo in CupsManager.Instance.CupsInfo)
		{
			LeagueElement league = Object.Instantiate(elementInScroll);
			league.transform.parent = base.transform;
			league.transform.localScale = Vector3.one;
			league.Init(cupsInfo);
			listLeague.Add(league);
			league.gameObject.SetActive(true);
		}
		grid.enabled = true;
		yield return null;
		scroll.ResetPosition();
	}

	private void RefreshLeague()
	{
		foreach (LeagueElement item in listLeague)
		{
			item.ShowIcon(true);
			if (item.nameLeague == CupsManager.CurrentLeague.ToString())
			{
				item.curLeagueBackground.gameObject.SetActive(true);
			}
			else
			{
				item.curLeagueBackground.gameObject.SetActive(false);
			}
		}
	}
}
